﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_PositionWorking : System.Web.UI.Page
    {
        private IEmployeeTypeBLL etBLL = new EmployeeTypeBLL();
        private IEmployeeBLL eBll = new EmployeeBLL();
        private IPositionBLL posBLL = new PositionBLL();
        Main main = new Main();
        SsMng ssMng = new SsMng();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadRadTreeList();
                //FillDataToEmpType();
            }
        }

        protected void LoadRadTreeList()
        {
            DataTable dt = posBLL.LoadPositionWorkingJobsites(null, "");
            string table = "";
            var ds = (from DataRow dr in dt.Rows select dr).ToList().OrderBy(o => o["ID"]).ThenBy(o => o["PARENT_ID"]);
            foreach (DataRow dr in ds)
            {
                table += dr["ID"] + "|" + dr["PARENT_ID"] + "\n";
            }
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();

            try
            {
                RadTreeView1.DataSource = dt;
                RadTreeView1.DataBind();
                RadTreeView1.Visible = true;

                if (Session["NodeSelectedValue"] != null)
                {
                    RadTreeNode selected_node = RadTreeView1.FindNodeByValue(Session["NodeSelectedValue"].ToString());

                    selected_node.ExpandParentNodes();
                    selected_node.Selected = true;
                    selected_node.Focus();
                } else
                {
                    RadTreeView1.ExpandAllNodes();
                }
                 
            } catch(Exception error)
            {
                throw error;
            } 
            // RadGrid1.Visible = true;
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            string strSearch = txtSearch.Text.Trim();

            foreach (RadTreeNode n in RadTreeView1.Nodes)
            {
                if (!String.IsNullOrEmpty(strSearch) && n.Text.Contains(strSearch))
                {
                    n.BackColor = System.Drawing.Color.Yellow;
                    n.ExpandParentNodes();
                }
                else
                    n.BackColor = System.Drawing.Color.Transparent;

                main.FindRecursive(n, strSearch);
            }
        }

        protected void RadTreeView1_NodeDrop(object sender, RadTreeNodeDragDropEventArgs e)
        {
            PositionInfo Info = new PositionInfo();

            foreach (RadTreeNode node in e.DraggedNodes)
            {
                RadTreeNode destNode = e.DestDragNode;

                string nodeUpper = destNode.Value;//nodeValueHiddenField.Value.Trim();
                //string[] root_value = nodeUpper.Split(new string[] { "-" }, StringSplitOptions.None);
                //string[] child_value = node.Value.Split(new string[] { "-" }, StringSplitOptions.None);

                //nodeUpper = nodeUpper == "" ? "0" : nodeUpper;

                int Seq = node.Value.Length > 0 ? Int32.Parse(node.Value) : 0;
                string Upper = nodeUpper.Length > 0 ? nodeUpper : null;

                Session["NodeSelectedValue"] = node.Value;
                posBLL.UpdateWorkingUpper(Seq, Upper, ssMng.EmpSeqI);
            }

            LoadRadTreeList();
        } 

        //protected void RadTreeView1_ContextMenuItemClick(object sender, RadTreeViewContextMenuEventArgs e)
        //{
        //    if(e.MenuItem.Value =="Manage")
        //    {
        //        var empSeq = e.Node.Value;
        //        radWindowWorkOnDuty.Visible = true;
        //        radWindowWorkOnDuty.NavigateUrl = "MP_PositionWorkOnDuty.aspx?do=Index&emp_seq=" + empSeq;
        //    }
        //} 

        //public void FillDataToEmpType()
        //{
        //    IList<EmployeeTypeInfo> InfoList = etBLL.ListEmployeeTypes(null);

        //    cmbEmpType.DataSource = InfoList;
        //    cmbEmpType.DataBind();
        //}
        //private void LoadRootNodes(RadTreeList treeView, TreeNodeExpandMode expandMode)
        //{
        //    //IList<TreeViewInfo> infoList = treeBLL.LoadJobSiteAreaTreeView("");
        //    //Nullable<int> EmpTypeSeq = cmbEmpType.SelectedValue.Length > 0 ? Int32.Parse(cmbEmpType.SelectedValue) : (int?)null;

        //    treeView.Nodes.Clear();

        //    var posBllPositionWorkingJobsites = posBLL.LoadPositionWorkingJobsites(null, "");

        //    foreach (TreeViewInfo info in posBllPositionWorkingJobsites)
        //    {
        //        RadTreeNode node = new RadTreeNode();
        //        node.Text = String.Format("[{0}] {1}", info.Id, info.Name);
        //        node.ToolTip = info.Descr;
        //        node.Value = info.Id;
        //        node.ImageUrl = info.Image_url;
        //        if (Convert.ToInt32(info.Children_count) > 0)
        //        {
        //            node.ExpandMode = expandMode;
        //        }
        //        treeView.Nodes.Add(node);
        //    }
        //}

        //private void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        //{
        //    e.Node.Dispose();
        //    e.Node.Nodes.Clear();

        //    //Nullable<int> EmpType = string.IsNullOrEmpty(cmbEmpType.SelectedValue) ? (int?)null : Int32.Parse(cmbEmpType.SelectedValue);

        //    var x = posBLL.LoadPositionWorkingJobsites(null, e.Node.Value);
        //    foreach (TreeViewInfo info in x)// treeBLL.LoadJobSiteAreaTreeView(e.Node.Value))
        //    {
        //        RadTreeNode node = new RadTreeNode();
        //        node.Text = String.Format("[{0}] {1}", info.Id, info.Name);
        //        node.ToolTip = info.Descr;
        //        node.Value = info.Id;
        //        node.ImageUrl = info.Image_url;
        //        if (Convert.ToInt32(info.Children_count) > 0)
        //        {
        //            node.ExpandMode = expandMode;
        //        }
        //        e.Node.Nodes.Add(node);
        //    }

        //    e.Node.Expanded = true;
        //    e.Node.ExpandMode = expandMode;
        //}

        //public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        //{
        //    //Nullable<int> EmpType = string.IsNullOrEmpty(cmbEmpType.SelectedValue) ? (int?)null : Int32.Parse(cmbEmpType.SelectedValue);

        //    foreach (TreeViewInfo info in posBLL.LoadPositionWorkingJobsites(null, e.Node.Value))
        //    {
        //        RadTreeNode node = new RadTreeNode();
        //        node.Text = info.Name;
        //        node.ToolTip = info.Descr;
        //        node.Value = info.Id;
        //        node.ImageUrl = info.Image_url;
        //        if (Convert.ToInt32(info.Children_count) > 0)
        //        {
        //            node.ExpandMode = expandMode;
        //        }
        //        e.Node.Nodes.Add(node);
        //    }

        //    e.Node.Nodes.Clear();
        //    e.Node.ExpandMode = expandMode;
        //}
        //protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        //{
        //    RadTreeNodeCollection nodes = this.RadTreeView1.Nodes;
        //    string strSearch = txtSearch.Text.TrimEnd();

        //    foreach (RadTreeNode n in nodes)
        //    {
        //        n.BackColor = RadTreeView1.BackColor;
        //        main.ClearRecursive(n, RadTreeView1.BackColor);
        //    }

        //    foreach (RadTreeNode n in nodes)
        //    {
        //        if (strSearch.Length > 0)
        //        {
        //            if (n.Text.IndexOf(strSearch) >= 0)
        //            {
        //                n.BackColor = System.Drawing.Color.Yellow;
        //                n.ExpandParentNodes();
        //            }
        //            main.FindRecursive(n, strSearch);
        //        }
        //    }
        //}

        //protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        //{
        //    RadTreeView1.ExpandAllNodes();
        //}

        //protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        //{
        //    RadTreeView1.CollapseAllNodes();
        //}
        //protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        //{
        //    PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);

        //}
        //protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        //{
        //    RadTreeNode selected_node = RadTreeView1.SelectedNode;

        //    string value = selected_node.Value;
        //    string name = selected_node.Text.Trim();
        //    string[] root_value = value.Split(new string[] { "-" }, StringSplitOptions.None);
        //    int nCount = root_value.Length;
        //    Session["TreeNodeValue"] = value;
        //    Session["TreeNodeSelect"] = value;
        //    if (!string.IsNullOrEmpty(value))
        //    {
        //        if (nCount > 1)
        //        {
        //            Session["TreeNodeValue"] = root_value[1];
        //            Session["POS_SEQ"] = Session["TreeNodeValue"].ToString();
        //            //OnUpdateCmdUI("VIEW");
        //        }
        //        else
        //        {
        //            //OnUpdateCmdUI("");
        //        }
        //    }
        //}
        //protected void RadTreeView1_NodeDrop(object sender, RadTreeNodeDragDropEventArgs e)
        //{
        //    PositionInfo Info = new PositionInfo();

        //    foreach (RadTreeNode node in e.DraggedNodes)
        //    {
        //        RadTreeNode destNode = e.DestDragNode;

        //        string nodeUpper = destNode.Value;//nodeValueHiddenField.Value.Trim();
        //        string[] root_value = nodeUpper.Split(new string[] { "-" }, StringSplitOptions.None);
        //        string[] child_value = node.Value.Split(new string[] { "-" }, StringSplitOptions.None);

        //        //nodeUpper = nodeUpper == "" ? "0" : nodeUpper;

        //        int Seq = child_value[1].Length > 0 ? Int32.Parse(child_value[1]) : 0;
        //        string Upper = root_value[1].Length > 0 ? root_value[1] : "";

        //        Session["TreeNodeSelect"] = node.Value;
        //        posBLL.UpdateWorkingUpper(Seq, Upper, ssMng.EmpSeqI);
                
        //    }

        //    RefreshTreeEdit();
        //}
        //public void RefreshTreeEdit()
        //{
        //    //RadTreeNode selected_node = RadTreeView1.SelectedNode;
        //    RadTreeNode selected_node = RadTreeView1.FindNodeByValue(Session["TreeNodeSelect"].ToString());

        //    if (selected_node == null)
        //    {
        //        selected_node = RadTreeView1.FindNodeByValue("0");
        //    }

        //    RadTreeNodeEventArgs node = new RadTreeNodeEventArgs(selected_node.ParentNode);
        //    RadTreeView1.CollapseAllNodes();
        //    PopulateNodeOnEditGrid(node, TreeNodeExpandMode.ServerSideCallBack);
        //    RadTreeView1.ExpandAllNodes();
        //    PopulateNodeOnDemand(node, TreeNodeExpandMode.ServerSideCallBack);

        //}
        //protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        //{
        //    LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
        //}

        //protected void cmbEmpType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        //{
        //    LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
        //}
    }
}
